<?php
namespace Home\Controller;
use Home\Controller\UserController;
use Common\Api\SendPhoneCodeApi;
use Common\Model\MemberModel;
class CurrencyController extends UserController {
public function _initialize(){
	parent::_initialize();
	}
	public function changeToLizi(){
		$currency_id=I('currency_id');
		$cu=M('member_money')->where('uid ='.$this->uid.' and currency_id ='.$currency_id)->find();
		$price=M('price')->find();
		if($cu['num']*$price['price'.$currency_id]<500){
			$data['status']=0;
			$data['info']='价值不足';
			$this->ajaxReturn($data);
		}
		 M()->startTrans();
		$r[]=M('member_money')->where('uid ='.$this->uid.' and currency_id ='.$currency_id)->setDec('num',$cu['num']);
		$r[]=$this->addFinance($this->uid, 2, '钱包划转到粒子', $cu['num'], 1,$currency_id);
		$r[]=M('member_money')->where('uid ='.$this->uid.' and currency_id ='.$currency_id)->setInc('num_lizi',$cu['num']);
		$r[]=$this->addFinance($this->uid, 1, '钱包划转到粒子', $cu['num'], 2,$currency_id);
		if($this->user['is_useful']==0){
			$r[]=M('member')->where('member_id ='.$this->uid)->setField('is_useful',1);
			$USER = new MemberModel();
			$up = $USER->getUserByUid($this->user['pid']);
			$this->addZtNum($up);
		}
		$arr['num']=$cu['num'];
		$arr['type']=1;
		$arr['currency_id']=$currency_id;
		$arr['add_time']=time();
		$arr['uid']=$this->uid;
		$r[]=M('lizi_log')->add($arr);
		$this->addTeamYeji($this->user,$cu['num'],$currency_id);
	 	if(in_array(false, $r)){
           M()->rollback();
           $data['status']=-7;
           $data['info']='操作失败';
           $this->ajaxReturn($data);
       	}else {
           M()->commit();
           $data['status']=1;
           $data['info']='操作成功';
           $this->ajaxReturn($data);
       	}
	}
	public function changeToWallet(){
		$currency_id=I('currency_id');
		$cu=M('member_money')->where('uid ='.$this->uid.' and currency_id ='.$currency_id)->find();
		$act_num=$cu['num_lizi']*(1-$this->config['change_fee']);
		M()->startTrans();
		$r[]=M('member_money')->where('uid ='.$this->uid.' and currency_id ='.$currency_id)->setInc('num',$act_num);
		$r[]=$this->addFinance($this->uid, 1, '粒子划转到钱包', $act_num, 1,$currency_id);
		$r[]=M('member_money')->where('uid ='.$this->uid.' and currency_id ='.$currency_id)->setDec('num_lizi',$cu['num_lizi']);
		$r[]=$this->addFinance($this->uid, 2, '粒子划转到钱包', $cu['num_lizi'], 2,$currency_id);
		$arr['num']=$cu['num_lizi'];
		$arr['type']=2;
		$arr['currency_id']=$currency_id;
		$arr['add_time']=time();
		$arr['uid']=$this->uid;
		$r[]=M('lizi_log')->add($arr);
		$this->reduceTeamYeji($this->user,$cu['num_lizi'],$currency_id);
		if(in_array(false, $r)){
			M()->rollback();
			$data['status']=-7;
			$data['info']='操作失败';
			$this->ajaxReturn($data);
		}else {
			M()->commit();
			$data['status']=1;
			$data['info']='操作成功';
			$this->ajaxReturn($data);
		}
	}
	public function exchangeConfig(){
		$data['fee']=$this->config['exchange_fee'];
		$price=M('price')->find();
		$data['price_eth']=$price['price2'];
		$data['price_par']=$price['price5'];
		$cu=M('member_money')->where('uid ='.$this->uid.' and currency_id = 5')->find();
		$data['max_num']=$cu['num'];
		$data['status']=1;
		$this->ajaxReturn($data);
	}
	public function getCurrencyDetail(){
		$currency_id=I('currency_id');
		$list=M('Currency')
			->alias('c')
			->field('c.*,cu.num')
			->join('__MEMBER_MONEY__ as cu on cu.currency_id = c.currency_id')
			->where('c.currency_id ='.$currency_id.' and cu.uid ='.$this->uid)
			->find();
		$data['list']=$list;
		$data['status']=1;
		$this->ajaxReturn($data);
	}
	public function exchange(){
		$pwd=I('pwd');
		$code=I('code');
		$num=I('num');
		$cu=M('member_money')->where('uid ='.$this->uid.' and currency_id = 5')->find();
		$price=M('price')->find();
		if(empty($pwd)){
			$data['status']=0;
			$data['info']='交易密码不能为空';
			$this->ajaxReturn($data);
		}
		if(empty($code)){
			$data['status']=0;
			$data['info']='验证码不能为空';
			$this->ajaxReturn($data);
		}
		if(empty($num)){
			$data['status']=0;
			$data['info']='兑换数量不能为空';
			$this->ajaxReturn($data);
		}
		if($num<0){
			$data['status']=-7;
			$data['info']='非法请求';
			$this->ajaxReturn($data);
		}
		if($num>$cu['num']){
			$data['status']=-7;
			$data['info']='数量不足';
			$this->ajaxReturn($data);
		}
		if($code != session('codeduihuan'.$this->user['phone'])){
			$data_return['status'] = -13;
			$data_return['info']   = '手机验证码有误';
			$this->ajaxReturn($data_return);
		}
		if(md5($pwd)!=$this->user['pwdtrade']){
			$data['status']=-7;
			$data['info']='交易密码错误';
			$this->ajaxReturn($data);
		}
		$eth_price=$price['price2'];
		$par_price=$price['price5'];
		$eth_num=round(($num*$par_price)*(1-$this->config['exchange_fee'])/$eth_price,4);
		M()->startTrans();
		$r[]=M('member_money')->where('uid ='.$this->uid.' and currency_id = 2')->setInc('num',$eth_num);
		$r[]=$this->addFinance($this->uid, 2, 'Par兑换', $eth_num, 1,2);
		$r[]=M('member_money')->where('uid ='.$this->uid.' and currency_id = 5')->setDec('num',$num);
		$r[]=$this->addFinance($this->uid, 2, 'Par兑换', $num, 1,5);
		$arr['num']=$num;
		$arr['act_num']=$eth_num;
		$arr['type']=2;
		$arr['add_time']=time();
		$arr['uid']=$this->uid;
		$r[]=M('exchange_log')->add($arr);
		if(in_array(false, $r)){
			M()->rollback();
			$data['status']=-7;
			$data['info']='操作失败';
			$this->ajaxReturn($data);
		}else {
			M()->commit();
			$data['status']=1;
			$data['info']='操作成功';
			$this->ajaxReturn($data);
		}
	}
	public function sendPhone(){
		$code_name = 'duihuan';
		$SENDPHONE = new SendPhoneCodeApi($this->user['phone']);
		$send = $SENDPHONE->send($code_name);
		if($send == 1){
			$data_return['status'] = 1;
			$data_return['info']   = '发送成功';
			return $this->ajaxReturn($data_return);
		}else{
			$data_return['status'] = -1;
			$data_return['info']   = $send;
			return $this->ajaxReturn($data_return);
		}
	}
	public function getProjectRule(){
		$list=M('Project_rule')->select();
		$data['list']=$list;
		$data['status']=1;
		$this->ajaxReturn($data);
	}
	public function addProject(){
		$num=I('num');
		$rule_id=I('rule_id');
		$currency_id=I('currency_id');
		$cu=M('member_money')->where('uid ='.$this->uid.' and currency_id ='.$currency_id)->find();
		if($num>$cu['num_lizi']){
			$data['status']=0;
			$data['info']='数量不足';
			$this->ajaxReturn($data);
		}
		if($num<=0){
			$data['status']=0;
			$data['info']='非法请求';
			$this->ajaxReturn($data);
		}
		$rule=M('project_rule')->where(["id"=>$rule_id])->find();
		M()->startTrans();
		$r[]=M('member_money')->where('uid ='.$this->uid.' and currency_id ='.$currency_id)->setDec('num_lizi',$num);
		$r[]=$this->addFinance($this->uid, 2, '锁仓', $num, 2,$currency_id);
		$r[]=M('member_money')->where('uid ='.$this->uid.' and currency_id ='.$currency_id)->setInc('num_suocang',$num);
		$r[]=$this->addFinance($this->uid, 1, '锁仓', $num, 3,$currency_id);
		$arr['num']=$num;
		$arr['currency_id']=$currency_id;
		$arr['all_day']=$rule['day_num'];
		$arr['bili']=$rule['bili'];
		$arr['add_time']=time();
		$arr['uid']=$this->uid;
		$r[]=M('project')->add($arr);
		if(in_array(false, $r)){
			M()->rollback();
			$data['status']=-7;
			$data['info']='操作失败';
			$this->ajaxReturn($data);
		}else {
			M()->commit();
			$data['status']=1;
			$data['info']='操作成功';
			$this->ajaxReturn($data);
		}
	}
	public function unlock(){
		$id=I('id');
		$project=M('project')->where('id ='.$id)->find();
		if(!$project){
			$data['status']=-7;
			$data['info']='非法请求';
			$this->ajaxReturn($data);
		}
		$act_num=$project['num']*0.95-$project['profit'];
		M()->startTrans();
		$r[]=M('project')->where('id ='.$id)->setField('status',-1);
		$r[]=M('member_money')->where('uid ='.$this->uid.' and currency_id ='.$project['currency_id'])->setInc('num_lizi',$act_num);
		$r[]=$this->addFinance($this->uid, 1, '解锁', $act_num, 2,$project['currency_id']);
		$r[]=M('member_money')->where('uid ='.$this->uid.' and currency_id ='.$project['currency_id'])->setDec('num_suocang',$project['num']);
		$r[]=$this->addFinance($this->uid, 2, '解锁', $project['num'], 3,$project['currency_id']);
		if(in_array(false, $r)){
			M()->rollback();
			$data['status']=-7;
			$data['info']='操作失败';
			$this->ajaxReturn($data);
		}else {
			M()->commit();
			$data['status']=1;
			$data['info']='操作成功';
			$this->ajaxReturn($data);
		}
	}
	public function getProjectLog(){
		$list=M('project')
			  ->alias('p')
			  ->field('p.*,c.currency_mark')
			  ->join('__CURRENCY__ as c on c.currency_id = p.currency_id')
			  ->where('p.uid ='.$this->uid)
			  ->select();
		$data['list']=$list;
		$data['status']=1;
		$this->ajaxReturn($data);
		
	}
	public function getCurrencyList(){
		$list=M('Currency')->field('currency_id,currency_name,currency_mark,currency_logo')->select();
		$data['list']=$list;
		$data['status']=1;
		$this->ajaxReturn($data);
	}
	public function getAddressList(){
		$currency_id=I('currency_id');
		$list=M('Currency_address')
			->alias('a')
			->field('a.*,c.currency_logo')
			->join('__CURRENCY__ as c on c.currency_id = a.currency_id')
			->where('a.currency_id ='.$currency_id.' and a.uid ='.$this->uid)
			->select();
		$data['list']=$list;
		$data['status']=1;
		$this->ajaxReturn($data);
	}
	public function addWalletAddress(){
		$currency_id=I('currency_id');
		$address=I('address');
		$info=I('info');
		$pwd=I('pwd');
		if(empty($address)){
			$data['status']=0;
			$data['info']='地址不能为空';
			$this->ajaxReturn($data);
		}
		if(empty($info)){
			$data['status']=0;
			$data['info']='备注不能为空';
			$this->ajaxReturn($data);
		}
		if(empty($pwd)){
			$data['status']=0;
			$data['info']='交易密码不能为空';
			$this->ajaxReturn($data);
		}
		if(md5($pwd)!=$this->user['pwdtrade']){
			$data['status']=0;
			$data['info']='交易密码错误';
			$this->ajaxReturn($data);
		}
		$arr['uid']=$this->uid;
		$arr['address']=$address;
		$arr['currency_id']=$currency_id;
		$arr['info']=$info;
		$arr['add_time']=time();
		$r=M('Currency_address')->add($arr);
		if($r){
			$data['status']=1;
			$data['info']='操作成功';
			$this->ajaxReturn($data);
		}else{
			$data['status']=0;
			$data['info']='操作失败';
			$this->ajaxReturn($data);
		}
	}
	public function getFinanceLog(){
		$page = I('page');
		$num = I('num');
		$currency_id=I('currency_id');
		$where['member_id']=$this->uid;
		$where['currency_id']=$currency_id;
		$tid=$this->uid%10;
		$pages = $this->DoPage($page, $num);
		$list=M('Finance_'.$tid)->where($where)->limit($pages['limit'],$pages['num'])->order('add_time desc')->select();
		$count =M('Finance_'.$tid)->where($where)->count();
		$data['list']=$list;
		$data['count']=$count;
		$data['status']=1;
		$this->ajaxReturn($data);
	}
	/**
	 * 超级节点
	 */
	public function addPoint(){
		$cu=M('member_money')->where('uid ='.$this->uid.' and currency_id = 2')->find();
		if($cu['num']<$this->config['point_price']){
			$data['status']=0;
			$data['info']='ETH不足';
			$this->ajaxReturn($data);
		}
		$check=M('point_log')->where('uid ='.$this->uid)->find();
		if($check){
			$data['status']=0;
			$data['info']='每个用户限申请一次';
			$this->ajaxReturn($data);
		}
		M()->startTrans();
		$r[]=M('member_money')->where('uid ='.$this->uid.' and currency_id = 2')->setDec('num',$this->config['point_price']);
		$r[]=$this->addFinance($this->uid, 2, '申请超级节点', $this->config['point_price'], 1,2);
		$arr['uid']=$this->uid;
		$arr['currency_id']=2;
		$arr['price']=$this->config['point_price'];
		$arr['num']=$this->config['point_price']*$this->config['point_lever'];
		$arr['bili']=$this->config['point_bili'];
		$arr['add_time']=time();
		$r[]=M('point_log')->add($arr);
		if(in_array(false, $r)){
			M()->rollback();
			$data['status']=-7;
			$data['info']='操作失败';
			$this->ajaxReturn($data);
		}else {
			M()->commit();
			$data['status']=1;
			$data['info']='操作成功';
			$this->ajaxReturn($data);
		}
	}
}